<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="VIP名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入机器人名称" clearable />
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="formData.name" placeholder="请输入价格" clearable />
                </el-form-item>
                <el-form-item label="所属角色" prop="ac_id">
                    <el-select
                        v-model="formData.ac_id"
                        class="flex-1"
                        placeholder="请选择所属角色"
                        clearable
                    >
                        <el-option
                            v-for="(item, index) in optionsData.companyList"
                            :key="index"
                            :label="item.c_name"
                            :value="item.ac_id"
                        />
                    </el-select>
                </el-form-item>            
                <el-form-item label="会员天数" >
                    <el-input v-model="formData.describe" placeholder="请输入会员天数" clearable />
                </el-form-item>
                <el-form-item label="token数量" prop="appid">
                    <el-input v-model="formData.appid" placeholder="请输入token数量" clearable />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="formData.disable" :active-value="0" :inactive-value="1" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
import { adminAdd, adminDetail, adminEdit } from '@/api/robot'
import { roleAll } from '@/api/perms/role'
import { companyListAll } from '@/api/company'
import { deptAll } from '@/api/org/department'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑机器人' : '新增机器人'
})
const formData = reactive({
    agent_id:'',
    ac_id: '',
    id: '',
    name: '',
    photo: '',
    describe: '',
    appid: '',
    disable: 0,
    app_key: '',
    dataset_key: '',
    type: 2,
    
})

const checkPhone = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('手机号不能为空'))
    } else {
        //验证手机号
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
            callback()
        } else {
            return callback(new Error('请输入正确的联系电话'))
        }
    }
}

const formRules = reactive({
    ac_id: [
        {
            type: 'number',
            required: true,
            message: '请选择所属企业',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    appid: [
        
        {
            required: true,
            message: '请输入appid',
            trigger: ['blur']
        }
    ],
    app_key: [
        {
            required: true,
            message: '请输入app_key',
            trigger: ['blur']
        }
    ],
   
})

const { optionsData } = useDictOptions<{
    role: any[]
    companyList: any[]
}>({
    role: {
        api: roleAll
    },
    companyList: {
        api: companyListAll
    }
    // dept: {
    //     api: deptAll
    // }
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formRules.password = []
    formRules.password_confirm = [
        {
            validator: passwordConfirmValidator,
            trigger: 'blur'
        }
    ]
    const data = await adminDetail({
        agent_id: row.agent_id
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const passwordConfirmValidator = (rule: object, value: string, callback: any) => {
    if (formData.password) {
        if (!value) callback(new Error('请再次输入密码'))
        if (value !== formData.password) callback(new Error('两次输入密码不一致!'))
    }
    callback()
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
